package com.hwpt.chesedemo.presentation.component;

import android.content.Context;
import android.content.SharedPreferences;


import dagger.hilt.android.qualifiers.ApplicationContext; // 新增导入

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * 蓝牙设备信息存储
 */

@Singleton
public class BtPreferences {
    
    private static final String PREF_NAME = "bt_demo_prefs";
    private static final String KEY_SAVED_DEVICE_ADDRESS = "saved_device_address";
    private static final String KEY_SAVED_DEVICE_NAME = "saved_device_name";
    private static final String KEY_SAVED_MTU = "saved_mtu";
    private final SharedPreferences prefs;

    @Inject
    public BtPreferences(@ApplicationContext Context context) {
        prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }
    
    /**
     * 保存设备信息
     */
    public void saveDevice(String address, String name) {
        prefs.edit()
            .putString(KEY_SAVED_DEVICE_ADDRESS, address)
            .putString(KEY_SAVED_DEVICE_NAME, name != null ? name : "未知设备")
            .apply();
    }
    
    /**
     * 获取保存的设备地址
     */
    public String getSavedDeviceAddress() {
        return prefs.getString(KEY_SAVED_DEVICE_ADDRESS, null);
    }
    
    /**
     * 获取保存的设备名称
     */
    public String getSavedDeviceName() {
        return prefs.getString(KEY_SAVED_DEVICE_NAME, null);
    }
    
    /**
     * 是否有保存的设备
     */
    public boolean hasSavedDevice() {
        return getSavedDeviceAddress() != null;
    }
    
    /**
     * 清除保存的设备
     */
    public void clearSavedDevice() {
        prefs.edit()
            .remove(KEY_SAVED_DEVICE_ADDRESS)
            .remove(KEY_SAVED_DEVICE_NAME)
            .remove(KEY_SAVED_MTU)
            .apply();
    }
    
    /**
     * 保存MTU值
     */
    public void saveMtu(int mtu) {
        prefs.edit()
            .putInt(KEY_SAVED_MTU, mtu)
            .apply();
    }
    
    /**
     * 获取保存的MTU值
     * @return 保存的MTU值，如果没有保存则返回0
     */
    public int getSavedMtu() {
        return prefs.getInt(KEY_SAVED_MTU, 27);
    }
    
    /**
     * 是否有保存的MTU
     */
    public boolean hasSavedMtu() {
        return getSavedMtu() > 0;
    }
}
